package com.mmall.controller;

import com.mmall.common.ParamCommon;
import com.mmall.common.ResponseService;
import com.mmall.common.ResultCode;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;


    /**
     * 用户登录接口
     * @param username      用户名
     * @param password      用户密码
     * @param session       session
     * @return              登录成功返回用户，失败返回提示。
     */
    @RequestMapping(value = "/login" , method = RequestMethod.POST)
    public ResponseService login(String username, String password, HttpSession session){

        ResponseService responseService = userService.login(username,password);
        if(responseService.isSuccess()){
            session.setAttribute(ParamCommon.KEY_USER,responseService.getData());
        }
        return responseService;
    }

    /**
     * 用户退出
     * @param session       session
     */
    @RequestMapping(value = "/logout")
    public ResponseService logout(HttpSession session){
        session.removeAttribute(ParamCommon.KEY_USER);
        return ResponseService.creteResponseSuccess();
    }

    /**
     * 用户注册接口
     * @param user          用户注册信息。username,password,email,phone,question,answer
     * @param session       session
     * @return              登录成功返回用户，失败返回提示。
     */
    @RequestMapping(value = "/register" , method = RequestMethod.POST)
    public ResponseService register(User user, HttpSession session){
        return userService.register(user);
    }

    /**
     * 检查用户名是否有效
     * @param str       可以是用户名也可以是email
     * @param type      对应的type是username和emai
     * @return          检验成功失败
     */
    @RequestMapping(value = "/check_valid")
    public ResponseService checkValid (String str, String type ){
        return userService.checkValid(str,type);
    }

    /**
     * 获取登录用户信息
     * @param session     session
     * @return            用户或者错误信息
     */
    @RequestMapping(value = "/get_user_info")
    public ResponseService getUserInfo (HttpSession session ){
        User user = (User) session.getAttribute(ParamCommon.KEY_USER);
        if(user == null ){
            return ResponseService.creteResponseSuccessByCode(ResultCode.USER_IS_EMPTY);
        }
        user.setQuestion(StringUtils.EMPTY);
        user.setAnswer(StringUtils.EMPTY);
        return ResponseService.creteResponseSuccessByData(user);
    }

    /**
     * 忘记密码 根据用户名获取提示问题
     * @param username      用户名
     * @return              问题或者错误信息
     */
    @RequestMapping(value = "/forget_get_question")
    public ResponseService forGetQuestion(String username){
        return userService.forGetQuestion(username);
    }

    /**
     * 检验用户回答提示问题是否正确
     * @param username      用户名
     * @param question      用户问题
     * @param answer        用户回答
     * @return              token或者错误信息
     */
    @RequestMapping(value = "/forget_check_answer" , method = RequestMethod.POST)
    public ResponseService forgetCheckAnswer(String username, String question ,String answer){
        return userService.CheckAnswer(username,question,answer);
    }
    /**
     * 修改用户密码
     * @param username      用户名
     * @param passwordNew   新密码
     * @param forgetToken   token
     * @return              修改成功或失败
     */
    @RequestMapping(value = "/forget_reset_password" , method = RequestMethod.POST)
    public ResponseService forgetResetPassword(String username, String passwordNew ,String forgetToken){
        return userService.resetPasswordByUserName(username,passwordNew,forgetToken);
    }


    /**
     * 修改用户密码
     * @param passwordOld   旧密码
     * @param passwordNew   新密码
     * @return              修改成功或失败
     */
    @RequestMapping(value = "/reset_password" , method = RequestMethod.POST)
    public ResponseService resetPassword(String passwordOld, String passwordNew, HttpSession session){
        User user = (User) session.getAttribute(ParamCommon.KEY_USER);
        if(user == null ){
            return ResponseService.creteResponseFailMsg("登录超时，请重新登录.");
        }
        return userService.resetPasswordByUsernamePass(user.getId(),passwordNew,passwordOld);
    }


    /**
     * 登录状态更新个人信息
     * @param user          用户信息
     * @return              成功 or 失败
     */
    @RequestMapping(value = "/update_information" , method = RequestMethod.POST)
    public ResponseService updateInformation(User user, HttpSession session){
        User cUser = (User) session.getAttribute(ParamCommon.KEY_USER);
        if(cUser == null ){
            return ResponseService.creteResponseSuccessByCode(ResultCode.USER_IS_EMPTY);
        }
        user.setId(cUser.getId());
        user.setUsername(cUser.getUsername());
        ResponseService response = userService.updateUserInfo(user);
        if(response.isSuccess()){
            session.setAttribute(ParamCommon.KEY_USER,response.getData());
        }
        return response;
    }

    /**
     * 获取当前登录用户的详细信息
     * @return              成功 or 失败
     */
    @RequestMapping(value = "/get_information" , method = RequestMethod.POST)
    public ResponseService getInformation( HttpSession session){
        User user = (User) session.getAttribute(ParamCommon.KEY_USER);
        if(user == null ){
            return ResponseService.creteResponseSuccessByCode(ResultCode.USER_IS_EMPTY);
        }
        return ResponseService.creteResponseSuccessByData(user);
    }
}
